package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.PaperClassDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.PaperClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PaperClassServiceImpl implements PaperClassService {
    @Autowired
    PaperClassDao paperClassDao;
    /**无条件查询所有，未使用*/
    @Override
    public ResponseEntity findAll() {
        List<PaperClassEntity> plist = paperClassDao.findAll();
        return new ResponseEntity<>(200,"success", plist);
    }


    /**查询总页数*/
    @Override
    public ResponseEntity findTotalNumber() {
          int nums=paperClassDao.findTotalNumber();
          return new ResponseEntity<>(200,"success", nums);
    }
    /**修改之前进行查询*/
    @Override
    public ResponseEntity updateBefore(PaperClassEntity paperClassEntity) {
        PaperClassEntity updateBeforeClass = paperClassDao.updateBefore(paperClassEntity);
        System.out.println("updateBefore"+updateBeforeClass.getId());
        if (updateBeforeClass != null) {
            return new ResponseEntity<>(200,"success", updateBeforeClass);
        }
        return ResponseEntity.FAIL;
    }
    /**按照当前页和每页条数查询数据*/
    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<PaperClassEntity> all = paperClassDao.findAll();
        int i =currentPage*5-4;
        for (PaperClassEntity paperClassEntity:all) {
            paperClassEntity.setFakeId(i++);
        }
        PageInfo<PaperClassEntity> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }
    /**根据id删除数据*/
    @Override
    public ResponseEntity deleteById(int id) {
        int i = paperClassDao.deleteById(id);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity findFid() {
        List<PaperClassEntity> findFidClass = paperClassDao.findFid();
        System.out.println("findFidClass"+findFidClass);
        /*考虑到无父类的情况*/
        PaperClassEntity nonFidEntity = new PaperClassEntity();
        nonFidEntity.setFid(0);
        nonFidEntity.setCategory("无父类");
        nonFidEntity.setId(0);
        System.out.println(nonFidEntity);
        findFidClass.add(nonFidEntity);

        if (findFidClass != null) {
            return new ResponseEntity<>(200,"success", findFidClass);
        }
        return ResponseEntity.FAIL;
    }

    /**新增数据*/
    @Override
    public ResponseEntity insertClass(PaperClassEntity paperClassEntity) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        paperClassEntity.setCreate_time(sf.format(date));
        paperClassEntity.setUpdate_time(sf.format(date));
        int i = paperClassDao.insertClass(paperClassEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**修改数据*/
    @Override
    public ResponseEntity updateClass(PaperClassEntity paperClassEntity) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        paperClassEntity.setUpdate_time(sf.format(date));
        int i = paperClassDao.updateClass(paperClassEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**批量删除*/
    @Override
    public ResponseEntity batchDelete(PaperClassEntity paperClassEntity) {
        int i = paperClassDao.batchDelete(paperClassEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**按试卷分类名称搜索*/
    @Override
    public ResponseEntity searchClass(PaperClassEntity paperClassEntity) {
        int currentPage=paperClassEntity.getCurrentPage();
        int pageSize=paperClassEntity.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<PaperClassEntity> searchlist = paperClassDao.searchClass(paperClassEntity);
        int i =currentPage*5-4;
        for (PaperClassEntity paperClass:searchlist) {
            paperClass.setFakeId(i++);
        }
        PageInfo<PaperClassEntity> pageInfo = PageInfo.of(searchlist);
        if (searchlist != null && searchlist.size() > 0) {
            System.out.println(pageInfo);
            return new ResponseEntity<>(200,"success", pageInfo);
        }
        return ResponseEntity.FAIL;
    }



}
